﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YTCMS.Core
{
    public class KeyLightHelper
    {
        IDictionary<char, List<string>> Dict { get; set; }

        /// <summary>
        /// 返回高亮的样式字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="keywords"></param>
        /// <param name="htmlTag"></param>
        /// <param name="cssName"></param>
        /// <returns></returns>
        public string GetLightCss(string source, string keywords, string htmlTag, string cssName)
        {
            if (string.IsNullOrEmpty(keywords) || string.IsNullOrEmpty(source))
            {
                return source;
            }
            FillDictionary(keywords);
            char[] tempString = source.ToCharArray();
            StringBuilder tbuider = new StringBuilder();
            int start = 0;
            for (; start < source.Length; start++)
            {
                int pause = start;
                if (Dict.ContainsKey(char.ToLower(source[start])))
                {
                    StringBuilder sb = new StringBuilder();
                    bool stop = false;
                    for (; start < source.Length; start++)
                    {
                        if (stop) { start--; break; }
                        sb.Append(source[start]);
                        foreach (string str in Dict[char.ToLower(source[pause])])      //查找匹配字符
                        {
                            if (str == sb.ToString().ToLower())
                            {
                                tbuider.AppendFormat("<{0} class=\"{1}\">", htmlTag, cssName).Append(sb).AppendFormat("</{0}>", htmlTag);
                                stop = true;
                                break;
                            }
                        }
                    }
                    if (!stop)
                    {
                        start = pause;
                        tbuider.Append(source[pause]);
                        continue;
                    }
                }
                else
                {
                    tbuider.Append(source[start]);
                }

            }
            return tbuider.ToString();
        }

        void FillDictionary(string keywords)
        {
            if (Dict != null) return;
            Dict = new Dictionary<char, List<string>>();
            keywords = keywords.Replace(' ', ',').ToLower();
            foreach (string item in keywords.Split(','))
            {
                if (string.IsNullOrEmpty(item)) { continue; }
                char c = item.ToLower().ToCharArray()[0];
                if (!Dict.ContainsKey(c))
                {
                    Dict.Add(c, new List<string>() { item });
                }
                else if (!Dict[c].Contains(item))
                {
                    Dict[c].Add(item);
                }
            }
        }
    }
}
